﻿using System;
using System.Collections.Generic;
using Jayden.Dll.Core.Enums;

namespace Jayden.Dll.Series
{
	public delegate bool FilterShowDelegate(ShowInfo show);
	public interface ShowInfoFilter
	{
		bool Filter(ShowInfo show);
	}
	public class CompositeShowInfoFilter : List<ShowInfoFilter>, ShowInfoFilter
	{
		public BooleanOperatorType Operator = BooleanOperatorType.And;

		public bool Filter(ShowInfo show)
		{
			foreach (ShowInfoFilter filter in this)
				if (Operator == BooleanOperatorType.And && !filter.Filter(show))
					return false;
				else if (Operator == BooleanOperatorType.Or && filter.Filter(show))
					return true;
			return Operator == BooleanOperatorType.And;
		}
	}
	public class ShowInfoCollection : HashSet<ShowInfo>
	{
		public ShowInfoCollection(IEnumerable<ShowInfo> shows) : base(shows) { }
		public ShowInfoCollection(IEnumerable<ShowInfo> shows, FilterShowDelegate filter)
		{
			foreach (ShowInfo show in shows)
				if (filter == null || filter(show))
					Add(show);
		}

		public ShowInfoCollection Filter(FilterShowDelegate filter) { return new ShowInfoCollection(this, filter); }
		public ShowInfoCollection Filter(ShowInfoFilter filter) { return Filter(filter.Filter); }

		public List<ShowInfo> Top(int count, Comparison<ShowInfo> comparison)
		{
			List<ShowInfo> list = new List<ShowInfo>(this);
			if(comparison != null)
				list.Sort(comparison);
			if (list.Count > count)
				list.RemoveRange(count, list.Count - count);
			return list;
		}
	}
}
